package indi.zn.commons.simplepoi;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;

public abstract class SSFBeanAbstract<T> implements SSFBeanConverter<T>{

	protected Class<T> clazz;

	protected String[] MatchFieldNames;

	protected Integer start;

	protected Integer end;

	protected Boolean formula = false;

	protected FormulaEvaluator hfe;
	
	protected Integer moveLines;
	protected Integer[] lastLines;
	
	protected SSFType type;
	
	protected WorkbookStyle ws;
	

	public WorkbookStyle getWs() {
		return ws;
	}

	public void setWs(WorkbookStyle ws) {
		this.ws = ws;
	}

	public void setMoveLines(Integer moveLines) {
		this.moveLines = moveLines;
	}

	public void setLastLines(Integer[] lastLines) {
		this.lastLines = lastLines;
	}
	
	public Class<T> getClazz() {
		return clazz;
	}

	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
		
	}

	
	public SSFBeanAbstract(Class<T> clazz,SSFType type){
		this.type=type;
	    this.clazz=clazz;
	    ws=new WorkbookStyle();
	}
	

	public List<T> xls2bean(InputStream is, String sheet) throws Exception {
		HSSFWorkbook workbook = new HSSFWorkbook(is);

		List<T> beanList = this.xls2bean(workbook, sheet);
		return beanList;

	}

	public List<T> xls2bean(byte[] buffer, String sheet) throws Exception {

		return this.xls2bean(new ByteArrayInputStream(buffer), sheet);

	}
	
	
	public static Object getValue(Cell cell, FormulaEvaluator hfe,
			Boolean formula) {
		Object value = null;
		Integer ct = null;
		if (cell != null) {
			ct = cell.getCellType();

			if (ct == 0) {
				value = new Double(cell.getNumericCellValue());
			} else if (ct == 1) {
				value = cell.getStringCellValue();
			} else if (ct == 2) {
				if (formula) {
					value = cell.getCellFormula();
				} else {
					
				
				try {
					CellValue cv = hfe.evaluate(cell);
				    value = cv.formatAsString();
				} catch (Exception e) {
					value="";
				 }
				}
			} else if (ct == 3) {
				value = "";
			} else if (ct == 4) {
				value = new Boolean(cell.getBooleanCellValue());
			} else if (ct == 5) {
				value = "ERROR";
			}
		}
		return value;
	}
	
	
	public static Cell setValue(Cell cell,Object cellvalue,Boolean formula){
		
		Integer ct = null;
		
			ct = cell.getCellType();
			
			
			/*		
			 *      CELL_TYPE_NUMERIC 数值型 0
					CELL_TYPE_STRING 字符串型 1
					CELL_TYPE_FORMULA 公式型 2
					CELL_TYPE_BLANK 空值 3
					CELL_TYPE_BOOLEAN 布尔型 4
					CELL_TYPE_ERROR 错误 5
			*/
			
			
			if (ct == 0) {
				if(cellvalue.getClass().getName().equals("java.lang.Double")){
					cell.setCellValue((Double)cellvalue);
				}
				else if(cellvalue.getClass().getName().equals("java.lang.Float")){
					cell.setCellValue((Float)cellvalue);		
				}
				else if(cellvalue.getClass().getName().equals("java.lang.Integer")){
					cell.setCellValue(cellvalue.toString());		
				}
				else if(cellvalue.getClass().getName().equals("java.lang.Long")){
					cell.setCellValue(cellvalue.toString());		
				}
			} else if (ct == 1) {
				
				if(cellvalue.getClass().getName().equals("java.util.Date")){
					cell.setCellValue((Date)cellvalue);
				}else{
				cell.setCellValue(cellvalue.toString());
				}
			} else if (ct == 2) {
				if (formula) {
					 cell.setCellFormula(cellvalue.toString());
				}	 
			} else if (ct == 3) {
				if(cellvalue.getClass().getName().equals("java.lang.Double")){
					cell.setCellValue((Double)cellvalue);
				}
				else if(cellvalue.getClass().getName().equals("java.lang.Float")){
					cell.setCellValue((Float)cellvalue);		
				}
				else if(cellvalue.getClass().getName().equals("java.lang.Integer")){
					cell.setCellValue(cellvalue.toString());		
				}
				else if(cellvalue.getClass().getName().equals("java.lang.Long")){
					cell.setCellValue(cellvalue.toString());		
				}else if(cellvalue.getClass().getName().equals("java.lang.String")){
					cell.setCellValue(cellvalue.toString());	
				}	
			} else if (ct == 4) {
				cell.setCellValue(Boolean.parseBoolean(cellvalue.toString()));
			} else if (ct == 5) {}
			
	
		return cell;
		
		
	}
	
	
	
	/**
	 * @param x
	 * @param y
	 * @param cell
	 * @param workbook
	 */
	protected void setCellStyle(int x,int y,Cell cell,Workbook workbook){
		
		CellStyle cs=ws.getCellStyle(x, y);
		if(cs!=null){
		
		org.apache.poi.ss.usermodel.CellStyle pcs=workbook.createCellStyle();
		
		Font font = workbook.createFont();

		if(cs.getFont_Boldweight()!=null)font.setBoldweight(cs.getFont_Boldweight());
		if(cs.getFont_Italic()!=null)font.setItalic(cs.getFont_Italic());
		if(cs.getFont_FontHeightInPoints()!=null)font.setFontHeightInPoints(cs.getFont_FontHeightInPoints());
		if(cs.getFont_FontName()!=null)font.setFontName(cs.getFont_FontName());
		if(cs.getFont_Underline()!=null)font.setUnderline(cs.getFont_Underline());
		if(cs.getFont_color()!=null)font.setColor(cs.getFont_color());
		
		if(cs.getCellAlignment()!=null)pcs.setAlignment(cs.getCellAlignment());
		if(cs.getCellVerticalAlignment()!=null)pcs.setVerticalAlignment(cs.getCellVerticalAlignment());
		if(cs.getCellBorders()!=null&&cs.getCellBorders()[0]!=null)pcs.setBorderTop(cs.getCellBorders()[0]);
		if(cs.getCellBorders()!=null&&cs.getCellBorders()[1]!=null)pcs.setBorderBottom(cs.getCellBorders()[1]);
		if(cs.getCellBorders()!=null&&cs.getCellBorders()[2]!=null)pcs.setBorderLeft(cs.getCellBorders()[2]);
		if(cs.getCellBorders()!=null&&cs.getCellBorders()[3]!=null)pcs.setBorderRight(cs.getCellBorders()[3]);
		if(cs.getCellBackgroundColor()!=null)pcs.setFillBackgroundColor(cs.getCellBackgroundColor());
		if(cs.getCellhiden()!=null)pcs.setHidden(cs.getCellhiden());
		if(cs.getCellWrapTex()!=null)pcs.setWrapText(cs.getCellWrapTex());
		if(cs.getCellBordersColor()!=null&&cs.getCellBordersColor()[0]!=null)pcs.setTopBorderColor(cs.getCellBordersColor()[0]);
		if(cs.getCellBordersColor()!=null&&cs.getCellBordersColor()[1]!=null)pcs.setBottomBorderColor(cs.getCellBordersColor()[1]);
		if(cs.getCellBordersColor()!=null&&cs.getCellBordersColor()[2]!=null)pcs.setLeftBorderColor(cs.getCellBordersColor()[2]);
		if(cs.getCellBordersColor()!=null&&cs.getCellBordersColor()[3]!=null)pcs.setRightBorderColor(cs.getCellBordersColor()[3]);
	
		pcs.setFont(font);
		
		cell.setCellStyle(pcs);
		
		
		}
	}
	
	
	
}
